﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.ApplicationPages;
using System.Web.UI.WebControls;
using Winwise.SPWebConfig.Core;
using System.Collections.Generic;
using Winwise.SPWebConfig.Core.XslModification;

namespace Winwise.SPWebConfig.UI
{
    public partial class AddEditXSLWebconfigModification : WebConfigDialogAdminPageBase
    {

        public void ApplyWebConfigModif(object sender, EventArgs e)
        {
            ApplyWebConfigModif(this.Selector.CurrentItem);
        }


        public void OnContextChange(object sender, EventArgs e)
        {
            if (this.Selector.CurrentItem != null)
            {
                SPWebApplication currentItem = this.Selector.CurrentItem;
                LinkManage.HRef = GetManageLink(currentItem);
                OnContextChange(currentItem);
            }
        }

        public override void FillWebConfigModif(WebConfigModification modif)
        {
            XSLWebConfigModification modifXSL = modif as XSLWebConfigModification;
            modifXSL.DisplayName = TBDisplayName.Text;
            modifXSL.XslTransformation = TBTransformation.Text;
            modifXSL.XslReverseTransformation = TBReverseTransformation.Text;

            modifXSL.Zones.Clear();
            foreach (ListItem checkbox in CheckBoxZones.Items)
            {
                if (checkbox.Selected)
                {
                    modifXSL.Zones.Add((SPUrlZone)Enum.Parse(typeof(SPUrlZone), checkbox.Text));
                }
            }
        }

        public override void UpdateMessage(string message)
        {
            LblResults.Text = message;
        } 

        public void AddModif(object sender, EventArgs e)
        {
            AddEditModif<XSLWebConfigModification>(this.Selector.CurrentItem);
        }


        public override void DataBind(WebConfigModification modif)
        {
            XSLWebConfigModification modifXSL = modif as XSLWebConfigModification;
            TBDisplayName.Text = modifXSL.DisplayName;
            TBTransformation.Text = modifXSL.XslTransformation;
            TBReverseTransformation.Text = modifXSL.XslReverseTransformation;

            CheckBoxZones.ClearSelection();
            foreach (var zone in modifXSL.Zones)
            {
                CheckBoxZones.Items.FindByText(zone.ToString()).Selected = true;
            }
        }


        public override void DataBindEmpty()
        {
            TBTransformation.Text = TBReverseTransformation.Text = @"
<?xml version=""1.0"" encoding=""utf-8""?>
<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""
    xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" exclude-result-prefixes=""msxsl"">
    <xsl:output method=""xml"" indent=""yes""/>
            
    <xsl:template match=""@* | node()"">
        <xsl:copy>
            <xsl:apply-templates select=""@* | node()""/>
        </xsl:copy>
    </xsl:template>
              
</xsl:stylesheet>";
        }

        public override void UpdateErrorMessage(string message)
        {
            LabelErrorMessage.Text = message;
        }
    }
}
